﻿// Shader-Based-Image-Processing (SBIP)
// http://code.google.com/p/sbip/
//
// Copyright © Frank Nagl, 2009-2011
// admin@franknagl.de
//
namespace SBIP.OptionsForms
{
    using System;
    using System.Windows.Forms;
    using DirectShowLib;
    /// <summary>
    /// The video form.
    /// </summary>
    public partial class VideoForm : Form
    {
        private readonly Processor processor;
        private VideoPlayer vp;

        /// <summary>
        /// Initializes a new instance of the <see cref="VideoForm"/> class.
        /// </summary>
        /// <param name="processor">The SBIP processor, which manages all 
        /// image processing routines.</param>
        public VideoForm(Processor processor)
        {            
            InitializeComponent();
            this.processor = processor;  
          
            toolTip.SetToolTip(btnPlay, "Play");
            toolTip.SetToolTip(btnStop, "Stop");

            toolTip.SetToolTip(btnRewind, "Rewind one second");
            toolTip.SetToolTip(btnSmallRewind, "Rewind one frame");
            toolTip.SetToolTip(btnForward, "Forward one second");
            toolTip.SetToolTip(btnSmallForward, "Forward one frame");
        }

        /// <summary>
        /// Updates the trackbar's value each frame. By first call trackbar 
        /// will be initialized.
        /// </summary>
        /// <param name="firstCall">If first call it has to be set to 
        /// <c>true</c>.</param>
        public void UpdateEachFrame(bool firstCall)
        {
            if (firstCall)
            {
                vp = processor.VideoPlayer;
                trackBar.Width = Options.Width - 10;
                trackBar.Maximum = (int)(vp.Duration * vp.FramesPerSecond);
                trackBar.LargeChange = vp.FramesPerSecond; // 1 sec
                trackBar.SmallChange = 1; // 1 frame
                btnPlay.Image = Resource.pause;
                toolTip.SetToolTip(btnPlay, "Pause");
            }

            lbTime.Text = vp.CurrentPositionAsTimeString;
            EnableButtons(vp.IsWinding == 0);
            if (vp != null && vp.CurrentState == VideoState.Playing)
            {
                trackBar.Value = (int)processor.VideoPlayer.CurrentFramePosition;
            }
        }

        private void EnableButtons(bool enable)
        {
            btnStop.Enabled = enable;
            btnPlay.Enabled = enable;
            btnRewind.Enabled = enable;
            btnSmallRewind.Enabled = enable;
            btnForward.Enabled = enable;
            btnSmallForward.Enabled = enable;            
        }

        private void Stop()
        {
            vp.Stop();
            btnStop.Enabled = false;
            btnPlay.Image = Resource.play;
            toolTip.SetToolTip(btnPlay, "Play");
        }

        private void Pause()
        {
            vp.Pause();
            btnStop.Enabled = true;
            btnPlay.Image = Resource.play;
            toolTip.SetToolTip(btnPlay, "Play");
        }

        private void Play()
        {            
            vp.Play();            
            btnStop.Enabled = true;
            btnPlay.Image = Resource.pause;
            toolTip.SetToolTip(btnPlay, "Pause");
        }

        #region Button events
        private void BtnStopClick(object sender, EventArgs e)
        {
           Stop();
        }

        private void BtnPlayClick(object sender, EventArgs e)
        {
            if (vp.CurrentState == VideoState.Playing)
            {
                Pause();
            }
            else
            {
                Play();
            }
        }

        private void BtnForwardClick(object sender, EventArgs e)
        {
            vp.StartWind(WindState.ForwardSecond);
            vp.EndWind();
        }

        private void BtnSmallForwardClick(object sender, EventArgs e)
        {
            vp.StartWind(WindState.ForwardFrame);
            vp.EndWind();
        }

        private void BtnRewindClick(object sender, EventArgs e)
        {
            vp.StartWind(WindState.RewindSecond);
            vp.EndWind();
        }

        private void BtnSmallRewindClick(object sender, EventArgs e)
        {
            vp.StartWind(WindState.RewindFrame);
            vp.EndWind();
        }
        #endregion Button events

        #region Trackbar events
        private void TrackBarScroll(object sender, EventArgs e)
        {
            vp.StartWind(WindState.NewPosition, trackBar.Value);
        }

        private void TrackBarMouseDown(object sender, MouseEventArgs e)
        {
            TrackBarScroll(null, null);
        }

        private void TrackBarMouseUp(object sender, MouseEventArgs e)
        {
            vp.EndWind();
        }

        private void TrackBarKeyUp(object sender, KeyEventArgs e)
        {
            // end tick sliding needs vp.EndWind()
            if (e.KeyCode == Keys.Left || 
                e.KeyCode == Keys.Right ||
                e.KeyCode == Keys.Up ||
                e.KeyCode == Keys.Down)
            {
                vp.EndWind();
            }
        }
        #endregion Trackbar events
    }
}
